package XML;


import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class ConfigHelper 
{

	/**
	 * Create dynamic a hibernate.hhbm.xml
	 * @param userNameDB
	 * @param passwordDB
	 * @throws Exception
	 */
	public static void writeXML(String userNameDB,String passwordDB) throws Exception
	{
		try 
		{

			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			// root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("hibernate-configuration");
			doc.appendChild(rootElement);

			// session-factory elements
			Element sessionFactory = doc.createElement("session-factory");
			rootElement.appendChild(sessionFactory);

			// shorten way
			// staff.setAttribute("id", "1");

			// URL elements
			Element propertyURL = doc.createElement("property");
			Attr attrURL = doc.createAttribute("name");
			attrURL.setValue("connection.url");
			propertyURL.setAttributeNode(attrURL);
			propertyURL.appendChild(doc.createTextNode("jdbc:mysql://localhost:3306/tagsDatabase"));
			sessionFactory.appendChild(propertyURL);

			// Driver elements
			Element propertyDriver = doc.createElement("property");
			Attr attrDriver = doc.createAttribute("name");
			attrDriver.setValue("connection.driver_class");
			propertyDriver.setAttributeNode(attrDriver);
			propertyDriver.appendChild(doc.createTextNode("com.mysql.jdbc.Driver"));
			sessionFactory.appendChild(propertyDriver);


			// UserName elements
			Element propertyUserName = doc.createElement("property");
			Attr attrUserName = doc.createAttribute("name");
			attrUserName.setValue("connection.username");
			propertyUserName.setAttributeNode(attrUserName);
			propertyUserName.appendChild(doc.createTextNode(userNameDB));
			sessionFactory.appendChild(propertyUserName);


			// password elements
			Element propertyPassword = doc.createElement("property");
			Attr attrPassword = doc.createAttribute("name");
			attrPassword.setValue("connection.password");
			propertyPassword.setAttributeNode(attrPassword);
			propertyPassword.appendChild(doc.createTextNode(passwordDB));
			sessionFactory.appendChild(propertyPassword);


			// connection elements
			Element propertyConnection = doc.createElement("property");
			Attr attrConnection = doc.createAttribute("name");
			attrConnection.setValue("connection.autocommit");
			propertyConnection.setAttributeNode(attrConnection);
			propertyConnection.appendChild(doc.createTextNode("true"));
			sessionFactory.appendChild(propertyConnection);

			// MySQLDialect elements
			Element propertyMySQLDialect = doc.createElement("property");
			Attr attrMySQLDialect = doc.createAttribute("name");
			attrMySQLDialect.setValue("dsialect");
			propertyMySQLDialect.setAttributeNode(attrMySQLDialect);
			propertyMySQLDialect.appendChild(doc.createTextNode("org.hibernate.dialect.MySQLDialect"));
			sessionFactory.appendChild(propertyMySQLDialect);

			// update table
			Element propertyUpdateTable = doc.createElement("property");
			Attr attrUpdateTable = doc.createAttribute("name");
			attrUpdateTable.setValue("hbm2ddl.auto");
			propertyUpdateTable.setAttributeNode(attrUpdateTable);
			propertyUpdateTable.appendChild(doc.createTextNode("update"));
			sessionFactory.appendChild(propertyUpdateTable);
			
			
			// mappingResourceFile elements
			Element mappingResourcePlugin = doc.createElement("mapping");
			Attr attrResourcePlugin = doc.createAttribute("resource");
			attrResourcePlugin.setValue("plugin.hbm.xml");
			mappingResourcePlugin.setAttributeNode(attrResourcePlugin);
			mappingResourcePlugin.appendChild(doc.createTextNode(""));
			sessionFactory.appendChild(mappingResourcePlugin);

			// mappingResourceUsers elements
			Element mappingResourceTags = doc.createElement("mapping");
			Attr attrResourceTags = doc.createAttribute("resource");
			attrResourceTags.setValue("tagedFiles.hbm.xml");
			mappingResourceTags.setAttributeNode(attrResourceTags);
			mappingResourceTags.appendChild(doc.createTextNode(""));
			sessionFactory.appendChild(mappingResourceTags);


			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
		
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
			transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Configuration DTD 3.0//EN");
			transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
			transformer.setOutputProperty(OutputKeys.INDENT,"yes");
			
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File("C:\\ourDropBox\\User\\hibernate.cfg.xml"));

			// Output to console for testing
			// StreamResult result = new StreamResult(System.out);

			transformer.transform(source, result);

			System.out.println("File saved!");
		} 
		catch (ParserConfigurationException pce) 
		{
			pce.printStackTrace();
		} 
		catch (TransformerException tfe) 
		{
			tfe.printStackTrace();
		}
	}
	public static void writeXmlClient(String userName,String password,String email,String FolderPath) throws Exception
	{
		try 
		{

			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			// root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("settingsClients");
			doc.appendChild(rootElement);

			// session-factory elements
			Element sessionFactory = doc.createElement("client");
			rootElement.appendChild(sessionFactory);

			// shorten way
			// staff.setAttribute("id", "1");

			// URL elements
			Element propertyURL = doc.createElement("userName");
			propertyURL.appendChild(doc.createTextNode(userName));
			sessionFactory.appendChild(propertyURL);

			// Driver elements
			Element propertyDriver = doc.createElement("password");
			propertyDriver.appendChild(doc.createTextNode(password));
			sessionFactory.appendChild(propertyDriver);


			// UserName elements
			Element propertyUserName = doc.createElement("email");
			propertyUserName.appendChild(doc.createTextNode(email));
			sessionFactory.appendChild(propertyUserName);


			// password elements
			Element propertyPassword = doc.createElement("folderPath");
			propertyPassword.appendChild(doc.createTextNode(FolderPath));
			sessionFactory.appendChild(propertyPassword);


		

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
		
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
			
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File("C:\\ourDropBox\\User\\ClientDetails.xml"));

			// Output to console for testing
			// StreamResult result = new StreamResult(System.out);

			transformer.transform(source, result);

			System.out.println("File saved!");
		} 
		catch (ParserConfigurationException pce) 
		{
			pce.printStackTrace();
		} 
		catch (TransformerException tfe) 
		{
			tfe.printStackTrace();
		}
	}
}